package m.jclecai.com.base;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import m.jclecai.com.R;
import m.jclecai.com.dialog.CPDialog;
import m.jclecai.com.dialog.LoadingDialog;
import m.jclecai.com.view.XLoading;

/**
 * Created by hp on 2017/3/14.
 */

public abstract class ISuperBaseFragment extends Fragment implements XLoading.OnRetryClickListener{
    Context mContext;
    LoadingDialog dialog;
    private Toast mToast;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public abstract void initData();

    public abstract void initView();

    public abstract void initListener();

    //右边按钮的文本 drawable -1 代表不加图案
    protected abstract void rightOptionText(String text,int drawable);

    //标题文本
    protected abstract void setTitle(String text,int drawable);

    //左边按钮的文本
    protected abstract void leftOptionText(String text,int drawable);

    //背景色
    protected abstract int getTitleBackgroundColor();

    protected void showAlertDialog(String title, String message, String positionText, String negativeText
            , View.OnClickListener position, View.OnClickListener negative) {
        CPDialog dialog = new CPDialog(getActivity());
        dialog.setTitle(title);
        dialog.setMessage(message);
        dialog.setPositiveButton(positionText, position);
        dialog.setNegativeButton(negativeText, negative);
        dialog.show();
    }

    public void showDialog() {
        if (dialog == null) {
            dialog = new LoadingDialog(getActivity());
        }
        dialog.show();
    }

    public void dissDialog() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }

    //直接使用v4 startActivityForResult 导致fargmentActivity 里面的子fragmentrequestCode改变
    public void startActivityForResult(Intent intent, int requestCode){
        getActivity().startActivityForResult(intent, requestCode);
    }

    XLoading mLoadingView;

    public void showLoadingLayout(ViewGroup viewGroup) {
        showLoadingLayout(viewGroup, null);
    }

    public void showLoadingLayout(ViewGroup viewGroup, int resId) {
        showLoadingLayout(viewGroup, setString(resId));
    }

    public void showLoadingLayout(ViewGroup viewGroup, String message) {
        initLoadingLayout(viewGroup);
        mLoadingView.setLoadingMessage(message);
        mLoadingView.showLoadingLayout();
    }

    public void initLoadingLayout(ViewGroup viewGroup) {
        if (mLoadingView == null) {
            if (viewGroup == null)
                return;
            mLoadingView = new XLoading(getActivity(), viewGroup);
        }
    }

    public void showInternetOffLayout() {
        showInternetOffLayout(null,null);
    }

    public void showInternetOffLayout(String message,String btnMessage) {
        if (mLoadingView != null) {
            mLoadingView.setOnRetryListener(this);
            mLoadingView.setInternetOffButtonMessage(btnMessage);
            mLoadingView.setInternetOffMessage(message);
            mLoadingView.showInternetOffLayout(btnMessage);
        }
    }

    public void showEmptyLayout() {
        if (mLoadingView != null)
            mLoadingView.setExceptionIcon(R.mipmap.ic_empty);
        showExceptionLayout(setString(R.string.custom_empty_text), null);
    }

    public void showExceptionLayout() {
        if (mLoadingView != null)
            mLoadingView.setExceptionIcon(R.mipmap.ic_warning);
        showExceptionLayout(setString(R.string.cp_api_failed), null);
    }

    public void showExceptionLayout(String message,String btnMessage) {
        if (mLoadingView != null) {
            mLoadingView.setOnRetryListener(this);
            mLoadingView.setOtherExceptionButtonMessage(btnMessage);
            mLoadingView.setOtherExceptionMessage(message);
            mLoadingView.showExceptionLayout(btnMessage);
        }
    }

    public void closeLoadingLayout() {
        if (mLoadingView != null)
            mLoadingView.hideAll();
    }

    //出现数据加载失败异常之后的重试
    @Override
    public void OnRetry(String btnMessage) {

    }

    public String setString(int resId){
        try {
            if(getActivity().isFinishing())
                return "";
            return getString(resId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 提示的Toast
     *
     * @param content 提示的内容
     */
    protected void DisPlay(String content) {
        try {
            if(!getActivity().isFinishing())
                Toast.makeText(getActivity(), content, Toast.LENGTH_SHORT).show();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 提示的Toast，不会重复；
     * @param content 提示的内容；
     */
    protected void DisPlayContent(String content){
        try {
            if(!getActivity().isFinishing()){

                if (mToast == null) {
                    mToast = Toast.makeText(getActivity(),"",Toast.LENGTH_SHORT);
                }
                mToast.setText(content);
                mToast.show();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
